// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.assets.service.impl;

import com.liuxinlong.common.AosException;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.constant.ThreadLocalConstants;
import com.liuxinlong.constant.TimeFormatConstants;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.assets.service.AssetsExpenseBudgetService;
import com.liuxinlong.modules.dao.AssetsDepartmentDao;
import com.liuxinlong.modules.dao.AssetsExpenseBudgetDao;
import com.liuxinlong.modules.dao.AssetsExpenseYearBudgetDao;
import com.liuxinlong.modules.dao.PatentDepartmentDao;
import com.liuxinlong.modules.entity.ExpenseBudget;
import com.liuxinlong.modules.entity.ExpenseYearBudget;
import com.liuxinlong.modules.entity.User;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.ThreadlocalUtils;
import com.liuxinlong.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 实验费预算管理业务接口层实现
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2024-01-11
 */
@Service
@Slf4j
public class AssetsExpenseBudgetServiceImpl implements AssetsExpenseBudgetService {

    @Autowired
    private AssetsExpenseBudgetDao assetsExpenseBudgetDao;

    @Autowired
    private AssetsDepartmentDao assetsDepartmentDao;

    @Autowired
    private AssetsExpenseYearBudgetDao assetsExpenseYearBudgetDao;

    @Autowired
    private PatentDepartmentDao patentDepartmentDao;

    @Override
    @Transactional
    public List<Map<String, Object>> pageExpenseBudgetInfo(Map<String, Object> queryParam) {
        int startNum = (int) queryParam.get("startNum");
        String year = (String) queryParam.get("year");
        List<ExpenseBudget> originalList = assetsExpenseBudgetDao.pageExpenseBudgetInfo(queryParam);
        List<Map<String, Object>> resultList = completeResult(originalList, startNum, year);
        return resultList;
    }

    @Override
    public int getExpenseBudgetInfoCount(Map<String, Object> queryParam) {
        return assetsExpenseBudgetDao.getExpenseBudgetInfoCount(queryParam);
    }

    @Override
    @Transactional
    public void addExpenseBudgetInfo(ExpenseBudget expenseBudget) {
        String id = SnowFlake.nextIdStr();
        expenseBudget.setId(id);
        expenseBudget.setYear(expenseBudget.getYear().substring(0, 4));
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        expenseBudget.setCreateUser(currentUser.getId());
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        expenseBudget.setCreateTime(currentTime);
        assetsExpenseBudgetDao.insert(expenseBudget);
    }

    @Override
    @Transactional
    public void updateExpenseBudgetInfo(ExpenseBudget expenseBudget) {
        String department = expenseBudget.getDepartment();
        if (StringUtils.isEmpty(department)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "修改实验费预算信息失败，部门信息不存在！");
        }
        String id = expenseBudget.getId();
        ExpenseBudget oldInfo = assetsExpenseBudgetDao.selectById(id);
        if (ObjectUtils.isEmpty(oldInfo)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "修改实验费预算信息失败，实验费预算信息不存在！");
        }
        expenseBudget.setCreateTime(oldInfo.getCreateTime());
        expenseBudget.setCreateUser(oldInfo.getCreateUser());
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        expenseBudget.setUpdateUser(currentUser.getId());
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        expenseBudget.setUpdateTime(currentTime);
        assetsExpenseBudgetDao.updateById(expenseBudget);

    }

    @Override
    @Transactional
    public void deleteExpenseBudgetInfo(String id) {
        ExpenseBudget assetsExpenseBudget = assetsExpenseBudgetDao.selectById(id);
        if (ObjectUtils.isEmpty(assetsExpenseBudget)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "删除失败，实验费预算信息不存在！");
        }
        assetsExpenseBudgetDao.deleteById(id);
    }

    @Override
    public List<Map<String, Object>> pageExpenseYearBudgetInfo(Map<String, Object> queryParam) {
        List<ExpenseYearBudget> yearBudgetList = assetsExpenseYearBudgetDao.pageExpenseYearBudgetInfo(queryParam);
        int sort = (int) queryParam.get("startNum") + 1;
        List<Map<String,Object>> dataList = new ArrayList<>(yearBudgetList.size());
        for (ExpenseYearBudget yearBudget:yearBudgetList) {
            Map<String,Object> item =ObjectUtils.object2Map(yearBudget);
            item.put("sort",sort);
            dataList.add(item);
            sort++;
        }
        return dataList;
    }

    @Override
    public int getExpenseYearBudgetInfoCount(Map<String, Object> queryParam) {
        return assetsExpenseYearBudgetDao.getExpenseYearBudgetInfoCount(queryParam);
    }

    @Override
    public void addExpenseYearBudgetInfo(ExpenseYearBudget yearBudget) {
        String id = SnowFlake.nextIdStr();
        yearBudget.setId(id);
        yearBudget.setYear(yearBudget.getYear().substring(0, 4));
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        yearBudget.setCreateUser(currentUser.getId());
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        yearBudget.setCreateTime(currentTime);
        assetsExpenseYearBudgetDao.insert(yearBudget);
    }

    @Override
    public void updateExpenseYearBudgetInfo(ExpenseYearBudget yearBudget) {
        String id = yearBudget.getId();
        ExpenseYearBudget oldInfo = assetsExpenseYearBudgetDao.selectById(id);
        if (ObjectUtils.isEmpty(oldInfo)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "修改实验费年度预算信息失败，实验费年度预算信息不存在！");
        }
        yearBudget.setCreateTime(oldInfo.getCreateTime());
        yearBudget.setCreateUser(oldInfo.getCreateUser());
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        yearBudget.setUpdateUser(currentUser.getId());
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        yearBudget.setUpdateTime(currentTime);
        assetsExpenseYearBudgetDao.updateById(yearBudget);
    }

    @Override
    public void deleteExpenseYearBudgetInfo(String id) {

    }

    /**
     * 补全数据
     *
     * @param originalList 原始数据
     * @param startNum     数据起始值
     * @return 补全数据列表
     */
    private List<Map<String, Object>> completeResult(List<ExpenseBudget> originalList, int startNum, String year) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Map<String, String>> assetsDepartmentMap = assetsDepartmentDao.getAssetsDepartmentMap();
        Map<String, Map<String, String>> departmentMap = patentDepartmentDao.getDepartmentMap();
        int sort = startNum + 1;
        for (ExpenseBudget expenseOrder : originalList) {
            Map<String, Object> item = ObjectUtils.object2Map(expenseOrder);
            item.put("sort", sort);
            if (StringUtils.isEmpty(expenseOrder.getId())) {
                item.put("amount", 0);
                item.put("year", year);
            }
            if (assetsDepartmentMap.containsKey(expenseOrder.getDepartment())) {
                Map<String, String> departmentInfo = assetsDepartmentMap.get(expenseOrder.getDepartment());
                item.put("departmentName", departmentMap.get(expenseOrder.getDepartment()).get("name"));
                item.put("departmentSn", departmentInfo.get("departmentSn"));
            }
            resultList.add(item);
            sort++;
        }
        return resultList;
    }

}
